package companyCategory;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ComCategoryReducer extends Reducer<Text, Text, Text, Text> {
    private Text result = new Text();

    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        Map<String, Integer> companyTypeCount = new HashMap<>();

        for (Text value : values) {
            String companyType = value.toString();
            companyTypeCount.put(companyType, companyTypeCount.getOrDefault(companyType, 0) + 1);
        }

        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, Integer> entry : companyTypeCount.entrySet()) {
            sb.append(entry.getKey()).append(":").append(entry.getValue()).append(" ");
        }

        result.set(sb.toString());
        context.write(key, result);
    }
}

